<?php

/**
 * @version 	0.9.0b
 * @package 	K2 Filter
 * @copyright 	Copyright (c) 2009 Piotr Konieczny. All rights reserved
 * @license 	GNU/GPL, see http://www.gnu.org/licenses/gpl-3.0.html
 * @type 		Module
 * @author 		Piotr Konieczny
 * @website 	http://www.smartwebstudio.com/
 * @email 		piotr@smartwebstudio.com
 */

defined('_JEXEC') or die('Restricted access');

class JElementExtraElements extends JElement {
	var $_name = 'ExtraElements';

	function fetchElement($name, $value, &$node, $control_name)
	{
		switch ($name) {
		
			case 'field_id' :
				return JElementExtraElements::_selectfield($name, $value, $node, $control_name);
		}
	}

	function _selectfield($name, $value, $node, $control_name)
	{
		$value = (int)$value;
		
		//! when starting for first time select first folder
		$first_save = ($this->_parent->get($name.'firstsave', 0)) ? true: false;
		if ($first_save) {
			$value = 1;
		}
		
		global $mainframe;
		
		$query = "SELECT t.id, t.name, t.value, t.type, t.group, t.published, t.ordering FROM #__k2_extra_fields AS t";
		$query2 = "SELECT g.id, g.name FROM #__k2_extra_fields_groups AS g LEFT JOIN #__k2_extra_fields t ON t.group = g.id";
		
		$db = &JFactory::getDBO();
		$db->setQuery($query);
		$extra_fields = ($db->loadObjectList());
		
		if ($extra_fields == '') echo '<script type="text/javascript">alert("'.JText::_('K2 not installed!').'");</script>';
		else {
		
			// sort by Extra field order
			// ref: http://stackoverflow.com/questions/124266/sort-object-in-php
			function cmp( $a, $b ) {
				if(  $a->ordering ==  $b->ordering ){ return 0 ; } 
				return ($a->ordering < $b->ordering) ? -1 : 1;
			}
			usort($extra_fields,'cmp');
		
			$db2 = &JFactory::getDBO();
			$db2->setQuery($query2);
			$extra_fields_groups = ($db2->loadObjectList());
		
		
			$html	 = '<table class="adminlist" cellspacing="0" >';
			$html	.= '<thead>';
			$html	.= '<tr>';
			$html 	.= '<th class="title" valign="middle">&nbsp;</th>';		
			$html	.= '<th class="title" valign="middle">'.JText::_('Name').'</th>';
			$html	.= '<th class="title" valign="middle">'.JText::_('Type').'</th>';
			//$html .= '<th class="title" valign="middle">'.JText::_('Type').'</th>';
			$html	.= '<th class="title" valign="middle">'.JText::_('Group').'</th>';
			$html	.= '<th class="title" valign="middle">'.JText::_('Order').'<img alt="" src="images/sort_desc.png"/></th>';
			$html	.= '<th class="title" valign="middle">'.JText::_('Published').'</th>';
			$html	.= '<th class="title" valign="middle">'.JText::_('id').'</th>';
			$html	.= '</tr>';
			$html	.= '</thead>';
		
			$html .= '<tbody>';
		
			foreach ($extra_fields as $which=>$extra_row) 
			{
				//$extra_row = get_object_vars($extra_row2);
				$html	.= '<tr class="row'.($which&1).'"';
				//$html	.= ($extra_row->published =='0') ? 'style="opacity:.5"' :'';
				$html	.= '>';
			
				$html	.= '<td align="center">';
				$html	.= '<input name="'.$control_name.'['.$name.']" type="radio" value="'.$extra_row->id.'" title="'.$extra_row->name.'" ';
				$html	.= ($value == $extra_row->id) ? 'checked="checked"' :'';
				$html	.= ($extra_row->published =='0') ? 'disabled="disabled"' :'';
				$html	.= '/>';
				$html	.= '</td>';
			
				$html	.= '<td><strong>';
				$html	.= '<a href="'.JURI::base().'index.php?option=com_k2&view=extraField&cid='.$extra_row->id.'">';
				$html	.= $extra_row->name;
				$html	.= '</a>';
				$html	.= '</strong></td>';
			
				//$html	.= '<td>';
				//$html	.= $extra_row->value;
				//$html	.= '</td>';
			
				$html	.= '<td>';
				$html	.= $extra_row->type;
				$html	.= '</td>';
			
				$html	.= '<td align="center">';
				//$html	.= $extra_row->group;
				$html	.= '<a href="'.JURI::base().'index.php?option=com_k2&view=extraFieldsGroup&cid=1">';
				$html	.= $extra_fields_groups[$which]->name;
				$html	.= '</a>';
				$html	.= '</td>';
			
				$html	.= '<td align="center" >';
				$html	.= $extra_row->ordering;
				$html	.= '</td>';

				$html	.= '<td align="center" >';
				$html	.= '<a href="'.JURI::base().'index.php?option=com_k2&view=extraFields">';
				$html	.= ($extra_row->published =='1') ?
					'<img border="0" alt="'.JText::_('Enabled').'" src="images/tick.png" />' :
					'<img border="0" alt="'.JText::_('Disabled').'" src="images/publish_x.png" / >' ;
				$html	.= '</a>';
				$html	.= '</td>';
			
				$html	.= '<td align="center">';
				$html	.= $extra_row->id;
				$html	.= '</td>';
			
				$html	.= '</tr>';
			}
			$html	.= '</table>';
		
			$html	.= '<table><tr><td colspan="3">&nbsp;</td></tr><tr>';
			$html	.= '<td>Go to </td>';
			$html	.= '<td class="jpane-toggler"><span><a href="'.JURI::base().'index.php?option=com_k2&view=extraFields">K2 Extra fields manager</a></span></td>';
			$html	.= '<td class="jpane-toggler"><span><a href="'.JURI::base().'index.php?option=com_k2&view=extraFieldsGroups">K2 Extra field groups manager</a></span></td>';
			$html	.= '</tr></table>';
			return $html;
		}
	}
}

?>